package com.zero.mms.controller;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.neusoft.chenanqi.dto.EmpSalDto;
import com.neusoft.chenanqi.service.ImportItemService;
import com.neusoft.chenanqi.util.ImportExcelUtil;
import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.basic.model.Pager;
import com.zero.basic.util.JsonCreator;

@Controller("importItemController")
@RequestMapping("importItem")
public class ImportItemController {
	
	@Inject
	private ImportItemService importItemService;

	@RequestMapping("/searchImportItem")
	@ResponseBody
	public Object searchImportItem(String depName, String empNo, String startMonth, String endMonth) {
		
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		try {
			Pager<EmpSalDto> empSalDtoPager = importItemService.listImportItem(depName, empNo, startMonth, endMonth);
			objectNode.putPOJO("response", empSalDtoPager);
			objectNode.put("message", "获取导入项目列表成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}
	
	@RequestMapping("/updateImportItem")
	@ResponseBody
	public Object updateEmpSal(String empNo, String month, 
			@RequestParam(value = "importItemName[]") String[] importItemName, 
			@RequestParam(value = "importItemValue[]") String[] importItemValue) {
		
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		try {
			importItemService.updateImportItem(empNo, month, importItemName, importItemValue);
			objectNode.put("message", "更新导入项目信息成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		
		return objectNode;
	}
	
	@RequestMapping("/addEmpImportItem")
	@ResponseBody
	public Object addEmpImportItem(String empId, String month, 
			@RequestParam(value = "importItemName[]") String[] importItemName, 
			@RequestParam(value = "importItemValue[]") String[] importItemValue) {
		
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		try {
			importItemService.updateImportItem(empId, month, importItemName, importItemValue);
			objectNode.put("message", "添加导入项目信息成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		
		return objectNode;
	}
	
	@RequestMapping("/importFromExcel")
	@ResponseBody
	public Object importFromExcel(String month, 
			@RequestParam(value = "file") CommonsMultipartFile file) {
		
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		try {
			List<EmpSalDto> empSalDtoList = ImportExcelUtil.importEmpSal(file.getInputStream());
			importItemService.importImportItem(month, empSalDtoList);
			objectNode.put("message", "导入成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
		} catch (NumberFormatException e) {
			objectNode.put("message", "文件中存在不合法字符");
			objectNode.put("error", SmsErrorConstant.INVALID_AUGUMENT);
			objectNode.put("statusCode", SmsStatusCode.INVALID_CODE);
		} catch (ArrayIndexOutOfBoundsException e) {
			objectNode.put("message", "文件格式不正确");
			objectNode.put("error", SmsErrorConstant.INVALID_AUGUMENT);
			objectNode.put("statusCode", SmsStatusCode.INVALID_CODE);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		
		return objectNode;
	}
	
}
